<?php
!function_exists('zeaialone') && exit('forbidden');
require_once ZEAI.'cache/config_sms.php';
$sms_onoff = json_decode($_SMS['sms_onoff'],true);

switch ($submitok) {
	case 'ajax_get_verify':
		if(!ifmob($mob)){
			json_exit(array('flag'=>0,'msg'=>'手机号码不正确'));
		}else{
			switch ($kind) {
				case 'forgetpass_tg':
					if(@!in_array('tg',$navarr))exit("<div style='font-size:30px;text-align:center;margin:50px'>推广功能暂未开启</div>");
					if (!$db->ROW(__TBL_TG_USER__,"id","mob='$mob' AND FIND_IN_SET('mob',RZ)")){
						json_exit(array('flag'=>0,'msg'=>'此手机未注册或未认证，无法找回'));	
					}
				break;
				case 'forgetpass_u':
					if (!$db->ROW(__TBL_USER__,"id","mob='$mob' AND FIND_IN_SET('mob',RZ)")){
						json_exit(array('flag'=>0,'msg'=>'此手机未注册或未认证，无法找回'));	
					}
				break;
				default:
					if ($db->ROW(__TBL_USER__,"id","mob='$mob' AND FIND_IN_SET('mob',RZ)"))json_exit(array('flag'=>0,'msg'=>'此手机号码已被注册，请更换'));
				break;
			}
		}
		if ( ($Temp_regyzmrenum > $_SMS['sms_yzmnum']) && $_SMS['sms_yzmnum']>0 )json_exit(array('flag'=>0,'msg'=>'你重复发送次数过多，无法再试'));
		$_SESSION['Zeai_cn__verify'] = cdstr(4);
		$rtn = Zeai_sendsms_authcode($mob,$_SESSION['Zeai_cn__verify']);
		//$rtn = 0;
		if ($rtn == 0){
			setcookie("Temp_regyzmrenum",$Temp_regyzmrenum+1,time()+720000,"/",$_ZEAI['CookDomain']);  
			$chkflag = 1;
			$content = '验证码发送成功，请注意查收手机短信';//.$_SESSION['Zeai_cn__verify']
		}else{
			$chkflag = 0;
			$content = "错误码：$rtn"."-".sms_error($rtn);
		}
		//sms end
		$_SESSION['Zeai_cn__mob'] = $mob;
		json_exit(array('flag'=>$chkflag,'msg'=>$content));
	break;
}
function request_post($url = '', $post_data = array()) {
    if (empty($url) || empty($post_data)) {return false;}
    $o = "";
    foreach ( $post_data as $k => $v ){$o.= "$k=" . urlencode( $v ). "&" ;}
    $post_data = substr($o,0,-1);
    $postUrl = $url;
    $curlPost = $post_data;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Content-Encoding: utf-8'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return $data;
}
function Zeai_sendsms_authcode($mobile,$str,$mbkind='authcode') {
	global $_SMS;
	//ali
	if($_SMS['sms_company']=='ali'){
		require_once ZEAI.'api/ali/sms/sendSms.php';
		$smsbk = ali_sendSms($mobile,$str,$mbkind);
		if($smsbk->Code=="OK"){
			return 0;
		}else{
			return $smsbk->Message;
		}
	}	
	//rcscloud
	$sid      = $_SMS['sms_sid'];
	$apikey   = $_SMS['sms_apikey'];
	$content  = "@1@=".$str;// 参数值，多个参数以“||”隔开 如:@1@=HY001||@2@=3281
	if ($mbkind == 'authcode'){
		$tplid = $_SMS['sms_tplid_authcode'];
	}elseif($mbkind == 'findpass'){
		$tplid = $_SMS['sms_tplid_findpass'];
	}elseif($mbkind == 'CrmQxHn'){	
		$tplid = $_SMS['sms_tplid_CrmQxHn'];
	}elseif($mbkind == 'huhuan'){	
		$tplid = $_SMS['sms_tplid_hh'];
		$str=explode('|WWW_A9A_CN|',$str);
		$str1=$str[0];$str2=$str[1];
		$content = "@1@=".$str1.'||@2@='.$str2;
	}else{exit;}
	$svr_rest = "http://api.rcscloud.cn:8030/rcsapi/rest";// rest请求地址  或使用IP：121.14.114.153
	$sign     = md5($sid.$apikey.$tplid.$mobile.$content);// 签名认证 Md5(sid+apikey+tplid+mobile+content) 
	$svr_url  = $svr_rest."/sms/sendtplsms.json";// 服务器接口路径
	$post_data= array();
	$post_data["sign"]      = $sign;
	$post_data["sid"]       = $sid;
	$post_data["tplid"]     = $tplid;
	$post_data["mobile"]    = $mobile;
	$post_data["content"]   = $content;
	$json_arr = json_decode(request_post($svr_url, $post_data));
	return $json_arr->code;
}
function sms_error($rtn,$kind='rcscloud'){
	global $_SMS;
	if($_SMS['sms_company']=='rcscloud'){
		switch ($rtn) {
			case 1001:$c = '短信平台账号被关闭';break;
			case 1003:$c = '单次发送号码过多';break;
			case 1005:$c = '账号签名参数有误';break;
			case 1006:$c = '短信平台IP签权失败';break;
			case 1007:$c = '短信平台余额不足';break;
			case 1014:$c = '短信平台未找到对应id短信模板';break;
			case 1015:$c = '短信平台对应id短信模板不可用';break;
			case 1022:$c = '同一个手机号码在单位时间内超过发送次数';break;
			default:$c = '其它异常，请参阅短信平台【常见返回码】';break;
		}
	}elseif($_SMS['sms_company']=='ali'){
		$c = $rtn;
	}
	return $c;
}
?>
